package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baidu.ueditor.ActionEnter;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.BussinessContent;
import com.sl.au.entity.BussinessInspection;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.User;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.BussinessContentService;
import com.sl.au.service.BussinessLevelService;
import com.sl.utils.MsgHelper;


/**
 * 百度编辑器Controller
 * 
 * @author yangdi
 *
 */

@RestController
@RequestMapping(value = "/ui/bussinessLevelBinding")
public class BussinessLevelBindingController extends BaseController {

	@Autowired
	BussinessLevelService bussinessLevelService;
	
	@Autowired
	BussinessContentService bussinessContentService;
	
	@Autowired
	private BusinesssellerBaseInfoService businesssellerBaseInfoService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("bussinessLevelBinding/bussinessLevelBindingindex");
		return modelAndView;
	}
	

	@RequestMapping(value="getBussinessLevelAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getBussinessLevelAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		List<BussinessLevel> node=null;
		node=bussinessLevelService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=bussinessLevelService.getNodeMap(node);
		map.put("code", 1);
		map.put("nodes", nodeMap);
		return map;
	}
	
	@RequestMapping(value = "getbaseinfopage", method = RequestMethod.GET)
	public Map<String, Object> getbaseinfopage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<BusinesssellerBaseInfo> businesssellerBaseInfo = this.businesssellerBaseInfoService.getPageByNoLevel(start, length, key, "createTime", Sort.Direction.DESC);
		int count = businesssellerBaseInfoService.getCountByNoLevel(key);
		map.put("data", businesssellerBaseInfo);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getbaseinfobylevelpage", method = RequestMethod.GET)
	public Map<String, Object> getbaseinfobylevelpage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String bussinessLevelId=request.getParameter("bussinessLevelId");
		List<BusinesssellerBaseInfo> businesssellerBaseInfo = this.businesssellerBaseInfoService.getPageByLevel(start, length, key, bussinessLevelId, "createTime", Sort.Direction.DESC);
		int count = businesssellerBaseInfoService.getCountByLevel(key, bussinessLevelId);
		map.put("data", businesssellerBaseInfo);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value="/binding",method = RequestMethod.POST)
	public Map<String,Object> binding(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		String bussinessLevelId = (String) map.get("bussinessLevelId");
		ArrayList<String> infoid = (ArrayList<String>) map.get("infoid");
		BussinessLevel bussinessLevel = bussinessLevelService.getRepository().findOne(bussinessLevelId);
		for(String BusinesssellerBaseInfoId : infoid) {
			  BusinesssellerBaseInfo baseInfo = businesssellerBaseInfoService.getRepository().findOne(BusinesssellerBaseInfoId);
			  baseInfo.setBussinessLevel(bussinessLevel);
			  businesssellerBaseInfoService.getRepository().save(baseInfo);
		}
		msg.put("msg", "商户区域设置成功!");
		return msg;
	};
	
	@RequestMapping(value="/unbinding",method = RequestMethod.POST)
	public Map<String,Object> unBinding(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> infoid = (ArrayList<String>) map.get("infoid");
		for(String BusinesssellerBaseInfoId : infoid) {
			BusinesssellerBaseInfo baseInfo = businesssellerBaseInfoService.getRepository().findOne(BusinesssellerBaseInfoId);
			baseInfo.setBussinessLevel(null);
			businesssellerBaseInfoService.getRepository().save(baseInfo);
		}
		msg.put("msg", "商户移除成功!");
		return msg;
	};
	
}
